package ratons.racleurs.main;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class QuestionActivity extends Activity {

	public ArrayList<String> poi ;
	public String etape ;
	public int i ;
	public int res ;
	public RadioGroup all ;
	public RadioButton check1 ;
	public RadioButton check2 ;
	public RadioButton check3 ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);
		
		Bundle objetbundle  = this.getIntent().getExtras();
	    // Récupération des informations du bundle
	    if (objetbundle != null && objetbundle.containsKey("poi") && objetbundle.containsKey("etape")) 
	    {
	    	poi = this.getIntent().getStringArrayListExtra("poi");
	    	etape = this.getIntent().getStringExtra("etape");
	    } else {
	    	poi = null;
	    	etape = "Error";
	    }
	    
	    System.out.println("Ok : " + poi.toString());
	    
	    TextView nom = (TextView)findViewById(R.id.nomPOI);
	    nom.setText(poi.get(0));
	    
	    TextView question = (TextView)findViewById(R.id.question);
	    question.setText(poi.get(1));
	    
	    all = (RadioGroup) findViewById(R.id.allradio);
	    check1 = (RadioButton) findViewById(R.id.radio1);
	    check2 = (RadioButton) findViewById(R.id.radio2);
	    check3 = (RadioButton) findViewById(R.id.radio3);
	    
	    Button bouton = (Button) findViewById(R.id.reponse);
	    
	    bouton.setOnClickListener(responseListener);
	    
	    int x = Integer.parseInt(etape);
	    
	    i = x % 3 ;
	    
	    if(i == 0){
	    	
	    	check1.setText(poi.get(2));
	    	check2.setText(poi.get(3));
	    	check3.setText(poi.get(4));
	    } else if (i == 1){
	    	res = 1 ;
	    	check1.setText(poi.get(3));
	    	check2.setText(poi.get(2));
	    	check3.setText(poi.get(4));
	    } else {
	    	res = 2 ;
	    	check1.setText(poi.get(4));
	    	check2.setText(poi.get(3));
	    	check3.setText(poi.get(2));
	    }
	    
	}

	private OnClickListener responseListener = new OnClickListener() 
    {
    	public void onClick(View v) 
    	{
    		System.out.println(all.getCheckedRadioButtonId()+"  "+i);
    		if((i == 0 && check1.isChecked()) || (i == 1 && check2.isChecked()) || (i == 2 && check3.isChecked())){
    			AlertDialog.Builder adb = new AlertDialog.Builder(QuestionActivity.this);

    	        adb.setTitle("Souhaitez vous prendre une photo commémorative ?");
    	 
    	        adb.setIcon(android.R.drawable.ic_dialog_alert);
    	 
    	        adb.setPositiveButton("Pas implémentée", new DialogInterface.OnClickListener() {
    	            public void onClick(DialogInterface dialog, int which) {
    	            	int x = Integer.parseInt(etape);
    	            	x ++ ;
    	            	etape = Integer.toString(x);
    	            	
    	            	Bundle donnees = new Bundle();
    	        		donnees.putString("etape", etape);
    	        		donnees.putStringArrayList("poi", poi);
    	        		Intent intent = new Intent(QuestionActivity.this, ParcoursActivity.class);
    	        		
    	        		intent.putExtras(donnees);

    	        		startActivity(intent);
    	          } });
    	 
    	        
    	        adb.setNegativeButton("Non merci", new DialogInterface.OnClickListener() {
    	            public void onClick(DialogInterface dialog, int which) {
    	            	
    	            	int x = Integer.parseInt(etape);
    	            	x ++ ;
    	            	etape = Integer.toString(x);
    	            	
    	            	Bundle donnees = new Bundle();
    	        		donnees.putString("etape", etape);
    	        		donnees.putStringArrayList("poi", poi);
    	        		Intent intent = new Intent(QuestionActivity.this, ParcoursActivity.class);
    	        		
    	        		intent.putExtras(donnees);

    	        		startActivity(intent);
    	          } });
    	        adb.show();
    		} else {
    			Toast.makeText(QuestionActivity.this, R.string.mauvaiseReponse, 3).show();
    		}
    	}
    };
}
